const Router = require('koa-router')
const { catchError, generateOk, check } = require('../../lib/check')
const { getAllHotSearches, searchHotSearch, addHotSearch, editHotSearch, removeHotSearch } = require('../../model/hotSearches')


let hotSearch = new Router()

hotSearch.post('/get', async (ctx) => {
  try {
    const data = ctx.request.body
    const hotSearches = getAllHotSearches()
    let newHotSearchs = hotSearches
    if (data.times === 'descend') {
      newHotSearchs = newHotSearchs.sort((a, b) => b.times - a.times)
    } else {
      newHotSearchs = newHotSearchs.sort((a, b) => a.times - b.times)
    }
    if (data.create_Ts === 'descend') {
      newHotSearchs = newHotSearchs.sort((a, b) => Number(b.create_Ts) - Number(a.create_Ts))
    } else {
      newHotSearchs = newHotSearchs.sort((a, b) => Number(a.create_Ts) - Number(b.create_Ts))
    }
    if (data.statu !== undefined) {
      newHotSearchs = newHotSearchs.filter((item) => item.statu === data.statu)
    }
    if (data.content !== undefined) {
      newHotSearchs = newHotSearchs.filter((item) => item.content.includes(data.content))
    }
    const start = data.offset === undefined ? 0 : data.offset
    const size = data.limit === undefined ? 7 : data.limit
    const resData = newHotSearchs.slice(start, start + size)
    ctx.body = generateOk({ data: resData, total: newHotSearchs.length, length: resData.length })
  } catch (e) {
    catchError(e, ctx)
  }
})

hotSearch.post('/add', async (ctx) => {
  try {
    const data = ctx.request.body
    check(!!data.content, 'Params_Lost')
    check(data.statu !== undefined, 'Params_Lost')
    const findHotSearch = searchHotSearch(data.content)
    check(!findHotSearch, 'HotSearch_Has_Been_Exist')
    const param = {
      ...data,
      times: 0,
      create_Ts: new Date().getTime().toString()
    }
    addHotSearch(param)
    ctx.body = generateOk(param)
  } catch (e) {
    catchError(e, ctx)
  }
})

hotSearch.post('/changeStatu', async (ctx) => {
  try {
    const data = ctx.request.body
    check(!!data.content, 'Params_Lost')
    // check(!!data.id, 'Params_Lost')
    // check(data.state !== undefined, 'Params_Lost')
    const findHotSearch = searchHotSearch(data.content)
    check(findHotSearch, 'Not_HotSearch')
    editHotSearch({ ...findHotSearch, statu: findHotSearch.statu === 0 ? 1 : 0 })
    ctx.body = generateOk({ ...data, state: !data.statu })
  } catch (e) {
    catchError(e, ctx)
  }
})

hotSearch.post('/remove', async (ctx) => {
  try {
    const data = ctx.request.body
    check(data.content, 'Params_Lost')
    const findHotSearch = searchHotSearch(data.content)
    check(findHotSearch, 'Not_HotSearch')
    removeHotSearch(data)
    ctx.body = generateOk({ ...findHotSearch, statu: 0 })
  } catch (e) {
    catchError(e, ctx)
  }
})


hotSearch.post('/clear', async (ctx) => {
  try {
    const data = ctx.request.body
    check(data.content, 'Params_lost')
    const findHotSearch = searchHotSearch(data.content)
    check(findHotSearch, 'No_Such_HotSearch')
    editHotSearch({ ...findHotSearch, times: 0 })
    ctx.body = generateOk({ ...findHotSearch, times: 0 })
  } catch (e) {
    catchError(e, ctx)
  }
})

hotSearch.post('/changeContent', async (ctx) => {
  try {
    const data = ctx.request.body
    check(data.originContent, 'Params_lost')
    check(data.newContent, 'Params_Lost')
    const findHotSearch = searchHotSearch(data.originContent)
    check(findHotSearch, 'No_Such_HotSearch')
    editHotSearch({ ...findHotSearch, content: data.newContent })
    ctx.body = generateOk({ ...findHotSearch, content: data.newContent })
  } catch (e) {
    catchError(e, ctx)
  }
})

module.exports = hotSearch